package com.gtw.distribution;

import com.gtw.modules.api.service.TbDividendsService;
import com.gtw.modules.api.service.TbUserService;
import com.gtw.modules.entity.TbDividends;
import com.gtw.modules.entity.TbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by wuye on 2018/4/23.
 */
@Service
public class IntegralClearingUtil {

    @Autowired
    private TbDividendsService tbDividendsService;

    @Autowired
    private TbUserService tbUserService;
    /**
     * 会员积分等级换算
     * @param user
     * @return
     */
    public boolean distribution5(TbUser user){
        boolean isresult = false;
        TbUser tbuser = tbUserService.queryByUid(user.getUuid());

        try {
            /**
             * 魅力值等级换算
             */
            TbDividends tbDividends1 = tbDividendsService.queryByLevel(1);
            if (tbuser.getCharmvalue() >= tbDividends1.getIntegralMin() && tbuser.getCharmvalue() <= tbDividends1.getIntegralMax()) {
                tbuser.setIntegrallevel(1);
                tbUserService.update(tbuser);
            }
            TbDividends tbDividends2 = tbDividendsService.queryByLevel(2);
            if (tbuser.getCharmvalue() >= tbDividends2.getIntegralMin() && tbuser.getCharmvalue() <= tbDividends2.getIntegralMax()) {
                tbuser.setIntegrallevel(2);
                tbUserService.update(tbuser);
            }
            TbDividends tbDividends3 = tbDividendsService.queryByLevel(3);
            if (tbuser.getCharmvalue() >= tbDividends3.getIntegralMin() && tbuser.getCharmvalue() <= tbDividends3.getIntegralMax()) {
                tbuser.setIntegrallevel(3);
                tbUserService.update(tbuser);
            }
            TbDividends tbDividends4 = tbDividendsService.queryByLevel(4);
            if (tbuser.getCharmvalue() >= tbDividends4.getIntegralMin() && tbuser.getCharmvalue() <= tbDividends4.getIntegralMax()) {
                tbuser.setIntegrallevel(4);
                tbUserService.update(tbuser);
            }
            TbDividends tbDividends5 = tbDividendsService.queryByLevel(5);
            if (tbuser.getCharmvalue() >= tbDividends5.getIntegralMin() && tbuser.getCharmvalue() <= tbDividends5.getIntegralMax()) {
                tbuser.setIntegrallevel(5);
                tbUserService.update(tbuser);
            }
            TbDividends tbDividends6 = tbDividendsService.queryByLevel(6);
            if (tbuser.getCharmvalue() >= tbDividends6.getIntegralMin() && tbuser.getCharmvalue() <= tbDividends6.getIntegralMax()) {
                tbuser.setIntegrallevel(6);
                tbUserService.update(tbuser);
            }
            TbDividends tbDividends7 = tbDividendsService.queryByLevel(7);
            if (tbuser.getCharmvalue() >= tbDividends7.getIntegralMin() && tbuser.getCharmvalue() <= tbDividends7.getIntegralMax()) {
                tbuser.setIntegrallevel(7);
                tbUserService.update(tbuser);
            }

            /**
             * 能量值积分换算
             */
            TbDividends tbDividendsl11 = tbDividendsService.queryByLevel2(1);
            if (tbuser.getEnergyvalue() >= tbDividendsl11.getIntegralMin() && tbuser.getEnergyvalue() <= tbDividendsl11.getIntegralMax()) {
                tbuser.setEnergyvaluelevel(1);
                tbUserService.update(tbuser);
            }
            TbDividends tbDividendsl22 = tbDividendsService.queryByLevel2(2);
            if (tbuser.getEnergyvalue() >= tbDividendsl22.getIntegralMin() && tbuser.getEnergyvalue() <= tbDividendsl22.getIntegralMax()) {
                tbuser.setEnergyvaluelevel(2);
                tbUserService.update(tbuser);
            }
            TbDividends tbDividendsl33 = tbDividendsService.queryByLevel2(3);
            if (tbuser.getEnergyvalue() >= tbDividendsl33.getIntegralMin() && tbuser.getEnergyvalue() <= tbDividendsl33.getIntegralMax()) {
                tbuser.setEnergyvaluelevel(3);
                tbUserService.update(tbuser);
            }
            TbDividends tbDividendsl44 = tbDividendsService.queryByLevel2(4);
            if (tbuser.getEnergyvalue() >= tbDividendsl44.getIntegralMin() && tbuser.getEnergyvalue() <= tbDividendsl44.getIntegralMax()) {
                tbuser.setEnergyvaluelevel(4);
                tbUserService.update(tbuser);
            }
            TbDividends tbDividendsl55 = tbDividendsService.queryByLevel2(5);
            if (tbuser.getEnergyvalue() >= tbDividendsl55.getIntegralMin() && tbuser.getEnergyvalue() <= tbDividendsl55.getIntegralMax()) {
                tbuser.setEnergyvaluelevel(5);
                tbUserService.update(tbuser);
            }
            isresult = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return isresult;
    }
}
